<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f7fa;
            min-height: 100vh;
        }

        .header {
            background: #1e3a8a;
            color: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
        }

        .header h1 {
            font-size: 1.5rem;
            font-weight: 600;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .username {
            font-weight: 500;
        }

        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 0.5rem 1rem;
            border-radius: 6px;
            cursor: pointer;
            transition: background 0.3s ease;
        }

        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .change-password-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 0.5rem 1rem;
            border-radius: 6px;
            cursor: pointer;
            margin-right: 0.5rem;
            transition: background 0.3s ease;
        }

        .change-password-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .main-container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        .upload-section {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            margin-bottom: 2rem;
        }

        .upload-section h2 {
            color: #333;
            margin-bottom: 1.5rem;
            font-size: 1.3rem;
        }

        .upload-area {
            border: 2px dashed #ddd;
            border-radius: 8px;
            padding: 2rem;
            text-align: center;
            transition: border-color 0.3s ease;
            cursor: pointer;
        }

        .upload-area:hover {
            border-color: #667eea;
        }

        .upload-area.dragover {
            border-color: #667eea;
            background: rgba(102, 126, 234, 0.05);
        }

        .upload-icon {
            font-size: 3rem;
            color: #ccc;
            margin-bottom: 1rem;
        }

        .upload-text {
            color: #666;
            margin-bottom: 1rem;
        }

        .file-input {
            display: none;
        }

        .upload-btn {
            background: #1e3a8a;
            color: white;
            border: none;
            padding: 0.75rem 1.5rem;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 500;
            transition: transform 0.2s ease;
        }

        .upload-btn:hover {
            transform: translateY(-1px);
        }

        .files-section {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        }

        .files-section h2 {
            color: #333;
            margin-bottom: 1.5rem;
            font-size: 1.3rem;
        }

        .files-table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }

        .files-table th,
        .files-table td {
            padding: 1rem;
            text-align: left;
            border-bottom: 1px solid #eee;
            white-space: nowrap;
            overflow: hidden;
        }

        .files-table th,
        .files-table td {
            text-align: center;
        }

        .files-table td:first-child {
            text-align: center;
        }

        .files-table th:nth-child(1) { width: 25%; }
        .files-table th:nth-child(2) { width: 25%; }
        .files-table th:nth-child(3) { width: 25%; }
        .files-table th:nth-child(4) { width: 25%; }

        .files-table th {
            background: #f8f9fa;
            font-weight: 600;
            color: #333;
        }

        .files-table tr:hover {
            background: #f8f9fa;
        }

        .file-name {
            color: #1e3a8a;
            cursor: pointer;
            text-decoration: none;
            font-weight: 500;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            display: inline-block;
            max-width: 100%;
            width: 100%;
        }

        .file-name:hover {
            color: #1e40af;
            text-decoration: underline;
        }

        .file-size {
            color: #666;
        }

        .file-time {
            color: #999;
            font-size: 0.9rem;
        }

        .file-actions {
            display: flex;
            gap: 0.5rem;
            justify-content: center;
        }

        .action-btn {
            padding: 0.4rem 0.8rem;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.85rem;
            transition: background 0.3s ease;
        }

        .download-btn {
            background: #28a745;
            color: white;
        }

        .download-btn:hover {
            background: #218838;
        }

        .delete-btn {
            background: #dc3545;
            color: white;
        }

        .delete-btn:hover {
            background: #c82333;
        }

        .empty-state {
            text-align: center;
            padding: 3rem;
            color: #666;
        }

        .empty-state .icon {
            font-size: 4rem;
            color: #ccc;
            margin-bottom: 1rem;
        }

        .loading {
            text-align: center;
            padding: 2rem;
            color: #666;
        }

        .success-message {
            background: #d4edda;
            color: #155724;
            padding: 1rem;
            border-radius: 6px;
            margin-bottom: 1rem;
            display: none;
        }

        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 1rem;
            border-radius: 6px;
            margin-bottom: 1rem;
            display: none;
        }

        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 0.5rem;
            margin-top: 2rem;
            padding: 1rem;
            background: #f8f9fa;
            border-top: 1px solid #e1e5e9;
        }

        .page-btn {
            background: white;
            border: 1px solid #e1e5e9;
            color: #333;
            padding: 0.5rem 1rem;
            border-radius: 6px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 0.9rem;
            min-width: 40px;
        }

        .page-btn:hover {
            background: #f8f9fa;
            border-color: #1e3a8a;
        }

        .page-btn.active {
            background: #1e3a8a;
            color: white;
            border-color: #1e3a8a;
        }

        .page-btn:disabled {
            background: #f5f5f5;
            color: #ccc;
            cursor: not-allowed;
            border-color: #e1e5e9;
        }

        .page-btn:not(.page-btn) {
            background: none;
            border: none;
            color: #666;
            cursor: default;
            min-width: auto;
        }

        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 1rem;
            }
            
            .main-container {
                padding: 0 1rem;
            }
            
            .upload-section,
            .files-section {
                padding: 1rem;
            }
            
            .files-table {
                font-size: 0.9rem;
            }
            
            .files-table th,
            .files-table td {
                padding: 0.5rem;
            }
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }

        .modal-content {
            background-color: white;
            margin: 10% auto;
            padding: 0;
            border-radius: 8px;
            width: 90%;
            max-width: 500px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.3);
        }

        .modal-header {
            padding: 1rem 1.5rem;
            border-bottom: 1px solid #e1e5e9;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal-header h3 {
            margin: 0;
            color: #333;
        }

        .close {
            color: #aaa;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            line-height: 1;
        }

        .close:hover {
            color: #333;
        }

        .modal-body {
            padding: 1.5rem;
        }

        .form-group {
            margin-bottom: 1rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            color: #333;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 0.75rem;
            border: 2px solid #e1e5e9;
            border-radius: 6px;
            font-size: 1rem;
            outline: none;
            transition: border-color 0.3s ease;
        }

        .form-group input:focus {
            border-color: #1e3a8a;
        }

        .modal-footer {
            padding: 1rem 1.5rem;
            border-top: 1px solid #e1e5e9;
            display: flex;
            justify-content: flex-end;
            gap: 1rem;
        }

        .btn {
            padding: 0.75rem 1.5rem;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .btn-primary {
            background: #1e3a8a;
            color: white;
        }

        .btn-primary:hover {
            background: #1e40af;
        }

        .btn-secondary {
            background: #6c757d;
            color: white;
        }

        .btn-secondary:hover {
            background: #5a6268;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <h1>文件管理系统</h1>
            <div class="user-info">
                <span class="username" id="username">用户</span>
                <button class="change-password-btn" onclick="showChangePasswordModal()">修改密码</button>
                <button class="logout-btn" onclick="logout()">退出登录</button>
            </div>
        </div>
    </div>

    <div class="main-container">
        <div class="upload-section">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem;">
                <h2>上传文件</h2>
                <div style="display: flex; gap: 1rem;">
                    <button onclick="scanFiles()" style="background: #1e3a8a; color: white; padding: 0.5rem 1rem; border: none; border-radius: 6px; cursor: pointer; font-size: 0.9rem;">
                        🔍 扫描文件
                    </button>
                    <a href="/" target="_blank" style="background: #1e3a8a; color: white; padding: 0.5rem 1rem; border-radius: 6px; text-decoration: none; font-size: 0.9rem;">
                        📁 查看公开文件列表
                    </a>
                </div>
            </div>
            <div class="upload-area" id="uploadArea">
                <div class="upload-icon">📁</div>
                <div class="upload-text">拖拽文件到此处或点击选择文件，文件将自动上传</div>
                <input type="file" id="fileInput" class="file-input" multiple>
                <button class="upload-btn" onclick="document.getElementById('fileInput').click()">
                    选择文件
                </button>
            </div>
            
            <div style="margin-top: 1.5rem; padding-top: 1.5rem; border-top: 1px solid #eee;">
                <div style="font-size: 0.9rem; color: #666;">
                    💡 提示：选择文件或拖拽文件后，系统将自动上传，无需额外确认
                </div>
            </div>
        </div>

        <div id="messageContainer"></div>

        <div class="files-section">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem;">
                <h2>文件列表</h2>
                <div id="fileCount" style="color: #666; font-size: 0.9rem;">
                    <!-- 文件数量信息将通过JavaScript动态生成 -->
                </div>
            </div>
            
            <!-- 搜索区域 -->
            <div style="margin-bottom: 1.5rem; padding: 1rem; background: #f8f9fa; border-radius: 6px;">
                <div style="display: flex; gap: 1rem; align-items: center;">
                    <input type="text" id="fileSearchInput" placeholder="搜索文件名..." 
                           style="flex: 1; padding: 0.75rem; border: 2px solid #e1e5e9; border-radius: 6px; font-size: 1rem; outline: none;">
                    <button onclick="searchFiles()" style="background: #667eea; color: white; border: none; padding: 0.75rem 1.5rem; border-radius: 6px; cursor: pointer; font-weight: 500;">
                        搜索
                    </button>
                    <button onclick="clearSearch()" style="background: #6c757d; color: white; border: none; padding: 0.75rem 1.5rem; border-radius: 6px; cursor: pointer; font-weight: 500;">
                        清除
                    </button>
                </div>
            </div>
            
            <div id="filesContainer">
                <div class="loading">加载中...</div>
            </div>
            
            <!-- 分页区域 -->
            <div id="paginationContainer" style="display: none;">
                <div class="pagination">
                    <!-- 分页按钮将通过JavaScript动态生成 -->
                </div>
            </div>
        </div>
    </div>

    <!-- 修改密码模态框 -->
    <div id="changePasswordModal" class="modal" style="display: none;">
        <div class="modal-content">
            <div class="modal-header">
                <h3>修改密码</h3>
                <span class="close" onclick="closeChangePasswordModal()">&times;</span>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label for="currentPassword">当前密码:</label>
                    <input type="password" id="currentPassword" placeholder="请输入当前密码">
                </div>
                <div class="form-group">
                    <label for="newPassword">新密码:</label>
                    <input type="password" id="newPassword" placeholder="请输入新密码（至少6位）">
                </div>
                <div class="form-group">
                    <label for="confirmPassword">确认新密码:</label>
                    <input type="password" id="confirmPassword" placeholder="请再次输入新密码">
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-secondary" onclick="closeChangePasswordModal()">取消</button>
                <button class="btn btn-primary" onclick="changePassword()">确认修改</button>
            </div>
        </div>
    </div>

    <script>
        let files = [];
        let currentPage = 1;
        let currentPagination = null;
        let currentSearch = '';

        // 页面加载时获取文件列表
        document.addEventListener('DOMContentLoaded', function() {
            loadFiles();
            updateUsername();
            
            // 搜索框回车键支持
            document.getElementById('fileSearchInput').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    searchFiles();
                }
            });

            // 点击模态框外部关闭
            document.getElementById('changePasswordModal').addEventListener('click', function(e) {
                if (e.target === this) {
                    closeChangePasswordModal();
                }
            });
        });

        // 更新用户名显示
        function updateUsername() {
            fetch('/files')
                .then(response => {
                    if (response.ok) {
                        // 用户已登录，获取用户信息
                        const username = document.getElementById('username');
                        username.textContent = 'admin'; // 简化处理
                    }
                })
                .catch(() => {
                    window.location.href = '/login';
                });
        }

        // 加载文件列表
        async function loadFiles(page = 1) {
            try {
                currentPage = page;
                const params = new URLSearchParams({
                    page: currentPage,
                    limit: 10
                });
                
                // 添加搜索参数
                if (currentSearch && currentSearch.trim()) {
                    params.append('search', currentSearch);
                }
                
                console.log('Loading files for page:', currentPage, 'search:', currentSearch);
                const response = await fetch(`/files?${params}`);
                if (response.ok) {
                    const data = await response.json();
                    console.log('Files data received:', data);
                    files = data.files;
                    currentPagination = data.pagination;
                    console.log('Pagination data:', currentPagination);
                    displayFiles();
                    displayPagination();
                } else {
                    window.location.href = '/login';
                }
            } catch (error) {
                console.error('加载文件失败:', error);
                showMessage('加载文件失败', 'error');
            }
        }

        // 搜索文件
        function searchFiles() {
            const searchValue = document.getElementById('fileSearchInput').value.trim();
            currentSearch = searchValue;
            currentPage = 1; // 搜索时回到第一页
            loadFiles(1);
        }

        // 清除搜索
        function clearSearch() {
            document.getElementById('fileSearchInput').value = '';
            currentSearch = '';
            currentPage = 1; // 清除搜索时回到第一页
            loadFiles(1);
        }

        // 扫描文件
        async function scanFiles() {
            try {
                const response = await fetch('/scan-files', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    }
                });
                
                const data = await response.json();
                
                if (data.success) {
                    showMessage(data.message, 'success');
                    loadFiles(1); // 重新加载文件列表，回到第一页
                } else {
                    showMessage(`扫描失败: ${data.message}`, 'error');
                }
            } catch (error) {
                console.error('扫描文件失败:', error);
                showMessage('扫描文件失败: 网络错误', 'error');
            }
        }

        // 显示分页
        function displayPagination() {
            console.log('displayPagination called, currentPagination:', currentPagination);
            
            if (!currentPagination || currentPagination.totalPages <= 1) {
                console.log('No pagination needed, hiding container');
                document.getElementById('paginationContainer').style.display = 'none';
                return;
            }

            const paginationContainer = document.getElementById('paginationContainer');
            const paginationDiv = paginationContainer.querySelector('.pagination');
            
            const { currentPage, totalPages, hasPrev, hasNext } = currentPagination;
            console.log('Pagination info:', { currentPage, totalPages, hasPrev, hasNext });
            
            let paginationHTML = '';
            
            // 上一页按钮
            paginationHTML += `
                <button class="page-btn" onclick="changePage(${currentPage - 1})" ${!hasPrev ? 'disabled' : ''}>
                    上一页
                </button>
            `;
            
            // 页码按钮
            for (let i = 1; i <= totalPages; i++) {
                if (i === 1 || i === totalPages || (i >= currentPage - 2 && i <= currentPage + 2)) {
                    paginationHTML += `
                        <button class="page-btn ${i === currentPage ? 'active' : ''}" onclick="changePage(${i})">
                            ${i}
                        </button>
                        `;
                } else if (i === currentPage - 3 || i === currentPage + 3) {
                    paginationHTML += '<span class="page-btn" style="border: none; background: none;">...</span>';
                }
            }
            
            // 下一页按钮
            paginationHTML += `
                <button class="page-btn" onclick="changePage(${currentPage + 1})" ${!hasNext ? 'disabled' : ''}>
                    下一页
                </button>
            `;
            
            console.log('Generated pagination HTML:', paginationHTML);
            paginationDiv.innerHTML = paginationHTML;
            paginationContainer.style.display = 'block';
            console.log('Pagination container displayed');
        }

        // 切换页面
        function changePage(page) {
            loadFiles(page);
        }

        // 显示文件列表
        function displayFiles() {
            const container = document.getElementById('filesContainer');
            
            // 更新文件数量信息
            if (currentPagination) {
                const { currentPage, totalPages, totalFiles } = currentPagination;
                const startIndex = (currentPage - 1) * 10 + 1;
                const endIndex = Math.min(currentPage * 10, totalFiles);
                document.getElementById('fileCount').textContent = `第 ${startIndex}-${endIndex} 条，共 ${totalFiles} 条`;
            }
            
            if (files.length === 0) {
                container.innerHTML = `
                    <div class="empty-state">
                        <div class="icon">📁</div>
                        <div>暂无文件</div>
                        <div style="font-size: 0.9rem; margin-top: 0.5rem;">上传第一个文件开始使用</div>
                    </div>
                `;
                return;
            }

            const table = `
                <table class="files-table">
                    <thead>
                        <tr>
                            <th>文件名</th>
                            <th>大小</th>
                            <th>上传时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        ${files.map(file => `
                            <tr>
                                <td>
                                    <a href="/download/${file.storedName}" class="file-name" download title="${file.filename}">
                                        ${file.filename}
                                    </a>
                                </td>
                                <td class="file-size">${formatFileSize(file.size)}</td>
                                <td class="file-time">${formatDate(file.uploadTime)}</td>
                                <td class="file-actions">
                                    <button class="action-btn download-btn" onclick="downloadFile('${file.storedName}', '${file.filename}')">
                                        下载
                                    </button>
                                    <button class="action-btn delete-btn" onclick="deleteFile(${file.id})">
                                        删除
                                    </button>
                                </td>
                            </tr>
                        `).join('')}
                    </tbody>
                </table>
            `;
            
            container.innerHTML = table;
        }

        // 文件上传处理
        const uploadArea = document.getElementById('uploadArea');
        const fileInput = document.getElementById('fileInput');

        // 拖拽上传
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });

        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('dragover');
        });

        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                uploadFiles(files);
            }
        });

        // 点击选择文件
        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                uploadFiles(e.target.files);
            }
        });

        // 直接上传文件
        async function uploadFiles(fileList) {
            const files = Array.from(fileList);
            let successCount = 0;
            let failCount = 0;

            // 显示上传状态
            const uploadText = document.querySelector('.upload-text');
            uploadText.innerHTML = `正在上传 ${files.length} 个文件...<br><small style="color: #666;">请稍候</small>`;

            for (let file of files) {
                const formData = new FormData();
                formData.append('file', file);

                try {
                    const response = await fetch('/upload', {
                        method: 'POST',
                        body: formData
                    });

                    const data = await response.json();
                    
                    if (data.success) {
                        successCount++;
                    } else {
                        failCount++;
                        showMessage(`文件 "${file.name}" 上传失败: ${data.message}`, 'error');
                    }
                } catch (error) {
                    failCount++;
                    showMessage(`文件 "${file.name}" 上传失败: 网络错误`, 'error');
                }
            }

            // 显示上传结果
            if (successCount > 0) {
                showMessage(`上传完成！成功: ${successCount} 个，失败: ${failCount} 个`, 'success');
                loadFiles(1); // 重新加载文件列表，回到第一页
            }

            // 重置上传区域显示
            uploadText.innerHTML = '拖拽文件到此处或点击选择文件，文件将自动上传';
            
            // 清空文件输入
            fileInput.value = '';
        }

        // 下载文件
        function downloadFile(storedName, filename) {
            const link = document.createElement('a');
            link.href = `/download/${storedName}`;
            link.download = filename;
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }

        // 删除文件
        async function deleteFile(fileId) {
            if (!confirm('确定要删除这个文件吗？')) {
                return;
            }

            try {
                const response = await fetch(`/files/${fileId}`, {
                    method: 'DELETE'
                });

                const data = await response.json();
                
                if (data.success) {
                    showMessage('文件删除成功！', 'success');
                    loadFiles(currentPage); // 重新加载文件列表，保持在当前页
                } else {
                    showMessage(`删除失败: ${data.message}`, 'error');
                }
            } catch (error) {
                showMessage('删除失败: 网络错误', 'error');
            }
        }

        // 显示消息
        function showMessage(message, type) {
            const container = document.getElementById('messageContainer');
            const messageDiv = document.createElement('div');
            messageDiv.className = type === 'success' ? 'success-message' : 'error-message';
            messageDiv.textContent = message;
            messageDiv.style.display = 'block';
            
            container.appendChild(messageDiv);
            
            // 3秒后自动隐藏
            setTimeout(() => {
                messageDiv.style.display = 'none';
                container.removeChild(messageDiv);
            }, 3000);
        }

        // 退出登录
        async function logout() {
            try {
                await fetch('/logout', { method: 'POST' });
                window.location.href = '/login';
            } catch (error) {
                console.error('退出登录失败:', error);
            }
        }

        // 显示修改密码模态框
        function showChangePasswordModal() {
            document.getElementById('changePasswordModal').style.display = 'block';
            // 清空输入框
            document.getElementById('currentPassword').value = '';
            document.getElementById('newPassword').value = '';
            document.getElementById('confirmPassword').value = '';
        }

        // 关闭修改密码模态框
        function closeChangePasswordModal() {
            document.getElementById('changePasswordModal').style.display = 'none';
        }

        // 修改密码
        async function changePassword() {
            const currentPassword = document.getElementById('currentPassword').value;
            const newPassword = document.getElementById('newPassword').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (!currentPassword || !newPassword || !confirmPassword) {
                showMessage('请填写所有密码字段', 'error');
                return;
            }

            if (newPassword.length < 6) {
                showMessage('新密码长度不能少于6位', 'error');
                return;
            }

            if (newPassword !== confirmPassword) {
                showMessage('新密码和确认密码不一致', 'error');
                return;
            }

            try {
                const response = await fetch('/change-password', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        currentPassword: currentPassword,
                        newPassword: newPassword
                    })
                });

                const data = await response.json();
                
                if (data.success) {
                    showMessage('密码修改成功！', 'success');
                    closeChangePasswordModal();
                } else {
                    showMessage(`密码修改失败: ${data.message}`, 'error');
                }
            } catch (error) {
                showMessage('密码修改失败: 网络错误', 'error');
            }
        }

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }

        // 格式化日期
        function formatDate(dateString) {
            const date = new Date(dateString);
            return date.toLocaleString('zh-CN');
        }
    </script>
</body>
</html> 